<?php

class PhotosController extends AppController
{

    var $paginate = array(
        'Photos' => array(
            'limit' => 25,
            'order' => array('Photos.date_publi' => 'desc')
    ));

    function membre_view()
    {
        $u['user'] = $this->Auth->user('username');
        $this->set($u);
    }

    function admin_upload()
    {
         $this->layout = NULL;
        $id = $this->Auth->user('username');
        header('content-type: application/json');

        $h = getallheaders();
        $o = new stdClass();
        $source = file_get_contents('php://input');

        if (isset($h['param-value']))
        {
            unlink('img/' . $id . '/' . $h['param-value']);
        }
        file_put_contents('img/'.$id . '/' . $h['file-name'], $source);
        $o->name = $h['file-name'];
        $o->content = '<img src="/Capt_art/img/' . $id . '/' . $h['file-name'].'"/>';
        echo json_encode($o);
    }

    function membre_editer()
    {

        //Si des données sont envoyées
        if ($this->request->is('post'))
        {
            $d = $this->request->data; // on les stockent dans $d
            $dir = IMAGES . $this->Auth->user('username');
            if (!file_exists($dir))
            {
                mkdir($dir, 0777);
            }
            $dir.=DS . date('Y');
            if (!file_exists($dir))
            {
                mkdir($dir, 0777);
            }
            $f = explode('.', $d['Photo']['lien']['name']);
            $ext = '.' . end($f);
            $filename = Inflector::slug(implode('.', array_slice($f, 0, -1)), '-');

            $d['Photo']['id'] = null;
            $d['Photo']['date_publi'] = date('Y/m/d/H:i:s');
            $d['Photo']['user_id'] = $this->Auth->user('id');
            $d['Photo']['lien'] = $this->Auth->user('username') . '/' . date('Y') . '/' . $filename . $ext;

            if ($this->Photo->save($d, true, array('lien', 'titre', 'date_publi', 'user_id')))
            { //Si la sauvegarde dans la base de données réussi 
                move_uploaded_file($this->request->data['Photo']['lien']['tmp_name'], $dir . DS . $filename . $ext);
                $this->Session->setFlash("Photo enregistrée", "notif");
            } else
            {
                $this->Session->setFlash("Echec de l'enregistrement", "notif", array('type' => 'error'));
            }
            App::import('Vendor', 'instagraph');
            $p = array();
            $id = $this->Auth->user('id');

            $p['photos'] = $this->Photo->find('first', array(
                'conditions' => array(
                    'user_id' => $id),
                'order' => array('Photo.date_publi' => 'desc')
            ));
            $this->set($p);

            try
            {
                $instagraph = Instagraph::factory('C:/wamp/www' . $this->webroot . 'app/webroot/img/' . $p['photos']['Photo']['lien'], 'C:/wamp/www' . $this->webroot . 'app/webroot/img/' . $p['photos']['Photo']['lien']);
            } catch (Exception $e)
            {
                echo $e->getMessage();
                die;
            }
            $instagraph->gotham();
        }
    }

}

?>
